package model

import (
	"gowork/performanceSharing/utils"
	"time"
)

type Order struct {
	Id int64 `xorm:"pk autoincr"`
	Amount int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ShdaingRecords struct {
	Id  int64 `xorm:"pk autoincr"`
	OrderId int64
	PerformanceOwnerId int64
	PerformanceOwnerName string
	SharedObjectId int64
	SharedObjectName string
	SharedAmount int64
	OperatorId int64
	OperatorName string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Shdaing struct {
	AdviserId int64
	Amount int64
	Proportion float64
}

func (shdaingRecords *ShdaingRecords)ShdaingPerformance(orderId int64, operatorId int64, sList []*Shdaing, t string)(map[string]interface{}, error){

	w := new(utils.Worker)

	var o Order
	x.Id(orderId).Get(&o)

	if t == "1"{
		for _, s := range sList {
			var sr ShdaingRecords
			sr.Id = w.GetId()
			sr.SharedObjectId = s.AdviserId
			sr.SharedAmount = o.Amount / int64(len(sList))
			sr.OperatorId = operatorId
			sr.CreatedAt = time.Now()
			sr.UpdatedAt = time.Now()
			x.Insert(&sr)
		}
	}else if t == "2"{
		for _, s := range sList {
			var sr ShdaingRecords
			sr.Id = w.GetId()
			sr.SharedObjectId = s.AdviserId
			sr.SharedAmount = s.Amount
			sr.OperatorId = operatorId
			sr.CreatedAt = time.Now()
			sr.UpdatedAt = time.Now()
			x.Insert(&sr)
		}
	}else {
		for _, s := range sList {
			var sr ShdaingRecords
			sr.Id = w.GetId()
			sr.SharedObjectId = s.AdviserId
			sr.SharedAmount = int64(float64(o.Amount) * s.Proportion)
			sr.OperatorId = operatorId
			sr.CreatedAt = time.Now()
			sr.UpdatedAt = time.Now()
			x.Insert(&sr)
		}
	}

	return map[string]interface{}{"message": "分享成功","code":200}, nil
}

